package logic

import (
	"context"
	"time"

	"mall/common/errorx"
	"mall/service/backend/model"
	"mall/service/backend/rpc/internal/svc"
	"mall/service/backend/rpc/types/backend"

	"github.com/zeromicro/go-zero/core/logx"
)

type SetMenuStatusLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSetMenuStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetMenuStatusLogic {
	return &SetMenuStatusLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SetMenuStatusLogic) SetMenuStatus(in *backend.SetMenuStatusRequest) (*backend.SetMenuStatusResponse, error) {
	if len(in.Ids) == 0 {
		return nil, errorx.Status(errorx.ERROR_MENU_ID_REQUIRED, "")
	}

	_, err := l.svcCtx.BackendMenu.UpdateBatch(l.ctx, in.Ids, model.M{
		model.BackendMenuColumns.Status:        uint8(in.Status),
		model.BackendMenuColumns.DelStatus:     uint8(in.Status),
		model.BackendMenuColumns.LastUpdatedBy: uint64(in.UpdatedBy),
		model.BackendMenuColumns.UpdatedAt:     time.Now(),
	})
	if err != nil && err != model.ErrNotUpdated {
		return nil, err
	}

	return &backend.SetMenuStatusResponse{}, nil
}
